package com.youbug.common.util;

import com.youbug.system.entity.User;
import org.springframework.util.ObjectUtils;

public class ThreadLocalUtils {

    private static ThreadLocal<User> userThreadLocal = new ThreadLocal<>();

    public static String getCurrentUsername(){
        User user = userThreadLocal.get();
        return !ObjectUtils.isEmpty(user)?user.getUsername():null;
    }

    public static User getCurrentUser(){
        return userThreadLocal.get();
    }

    public static void setCurrentUser(User user){
        userThreadLocal.set(user);
    }
    public static Long getCurrentUserId(){
        User user = userThreadLocal.get();
        return !ObjectUtils.isEmpty(user)?user.getId():null;
    }
}
